﻿using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BSF.Extensions;
using BSF.Db;
using {t_namespace}.Model;

namespace {t_namespace}.Dal
{
	/*代码自动生成工具自动生成,不要在这里写自己的代码，否则会被自动覆盖哦 - 车毅*/
	public partial class {t_object}_dal
    {
        public virtual bool Add(DbConn PubConn, {t_object}_model model)
        {

            List<ProcedureParameter> Par = new List<ProcedureParameter>()
                {
					<!-- BEGIN FieldList1 -->
					//{f_note1}
					new ProcedureParameter("@{f_name1}",    model.{f_name_model1}),<!-- END FieldList1 -->   
                };
            int rev = PubConn.ExecuteSql(@"insert into {t_object}(<!-- BEGIN FieldList2 -->{f_name2},<!-- END FieldList2 -->)
										   values(<!-- BEGIN FieldList3 -->@{f_name3},<!-- END FieldList3 -->)", Par);
            return rev == 1;

        }

        public virtual bool Edit(DbConn PubConn, {t_object}_model model)
        {
            List<ProcedureParameter> Par = new List<ProcedureParameter>()
            {
                    <!-- BEGIN FieldList4 -->
					//{f_note4}
					new ProcedureParameter("@{f_name4}",    model.{f_name_model4}),<!-- END FieldList4 -->
            };
			Par.Add(new ProcedureParameter("@{t_mainkeyname}",  model.{t_idFiled}));

            int rev = PubConn.ExecuteSql("update {t_object} set <!-- BEGIN FieldList5 -->{f_name5}=@{f_name5},<!-- END FieldList5 --> where {t_mainkeyname}=@{t_mainkeyname}", Par);
            return rev == 1;

        }

        public virtual bool Delete(DbConn PubConn, {t_mainkeytype} {t_mainkeyname})
        {
            List<ProcedureParameter> Par = new List<ProcedureParameter>();
            Par.Add(new ProcedureParameter("@{t_mainkeyname}",  {t_mainkeyname}));

            string Sql = "delete from {t_object} where {t_mainkeyname}=@{t_mainkeyname}";
            int rev = PubConn.ExecuteSql(Sql, Par);
            if (rev == 1)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public virtual {t_object}_model Get(DbConn PubConn, {t_mainkeytype} {t_mainkeyname})
        {
            List<ProcedureParameter> Par = new List<ProcedureParameter>();
            Par.Add(new ProcedureParameter("@{t_mainkeyname}", {t_mainkeyname}));
            StringBuilder stringSql = new StringBuilder();
            stringSql.Append(@"select s.* from {t_object} s where s.{t_mainkeyname}=@{t_mainkeyname}");
            DataSet ds = new DataSet();
            PubConn.SqlToDataSet(ds, stringSql.ToString(), Par);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
				return CreateModel(ds.Tables[0].Rows[0]);
            }
            return null;
        }

		public virtual {t_object}_model CreateModel(DataRow dr)
        {
            var o = new {t_object}_model();
			<!-- BEGIN FieldList6 -->
			//{f_note6}
			if(dr.Table.Columns.Contains("{f_name6}"))
			{
				o.{f_name_model6} = dr["{f_name6}"].To{f_type6}();
			}<!-- END FieldList6 -->
			return o;
        }
    }
}